﻿using GalaSoft.MvvmLight;

namespace Budgetsch.WP7.ViewModel.Income
{
    using System;
    using System.Collections.ObjectModel;

    using Budgetsch.WP7.Navigation;
    using Budgetsch.WP7.ViewModel.Comment;
    using Budgetsch.WP7.WP7DataService;

    using GalaSoft.MvvmLight.Messaging;

    /// <summary>
    /// This class contains properties that a View can data bind to.
    /// <para>
    /// Use the <strong>mvvminpc</strong> snippet to add bindable properties to this ViewModel.
    /// </para>
    /// <para>
    /// See http://www.galasoft.ch/mvvm/getstarted
    /// </para>
    /// </summary>
    public class IncomeDetailsViewModel : ViewModelBase
    {
        /// <summary>
        /// The <see cref="Income" /> property's name.
        /// </summary>
        public const string IncomePropertyName = "Income";

        private Income _income = null;

        /// <summary>
        /// Sets and gets the Income property.
        /// Changes to that property's value raise the PropertyChanged event. 
        /// </summary>
        public Income Income
        {
            get
            {
                return _income;
            }

            set
            {
                if (_income == value)
                {
                    return;
                }

                _income = value;
                RaisePropertyChanged(IncomePropertyName);
            }
        }

        /// <summary>
        /// The <see cref="CommentViewModel" /> property's name.
        /// </summary>
        public const string CommentViewModelPropertyName = "CommentViewModel";

        private CommentViewModel _commentViewModel = null;

        /// <summary>
        /// Sets and gets the CommentViewModel property.
        /// Changes to that property's value raise the PropertyChanged event. 
        /// </summary>
        public CommentViewModel CommentViewModel
        {
            get
            {
                return _commentViewModel;
            }

            set
            {
                if (_commentViewModel == value)
                {
                    return;
                }

                _commentViewModel = value;
                RaisePropertyChanged(CommentViewModelPropertyName);
            }
        }

        /// <summary>
        /// Initializes a new instance of the IncomeDetailsViewModel class.
        /// </summary>
        public IncomeDetailsViewModel()
        {

            Messenger.Default.Register<LoadIncomeDetailsViewMessage>(this, m =>
                {
                    Income = m.Income;
                    CommentViewModel = new CommentViewModel(Income.IncomeComments);
                });
            Messenger.Default.Register<RefreshIncomeDetailsViewMessage>(
                this,
                m => this.CommentViewModel.CommentDetailsViewModels.Add(new CommentDetailsViewModel(m.Comment)));

            if (IsInDesignModeStatic)
            {
                Income = new Income
                    {
                        Amount = 3000,
                        IsRecurring = true,
                        StartDate = new DateTime(2011, 5, 11),
                        EndDate = new DateTime(2023, 11, 11),
                        Delta = 4,
                        IncomeCategory = new IncomeCategory { Name = "Fizetés" },
                        Description = "BME projekt pénz",
                        IncomeComments =
                            new ObservableCollection<IncomeComment>
                                {
                                    new IncomeComment
                                        { Comment = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Morbi auctor leo pretium nibh imperdiet id rhoncus ante cursus. Praesent et sapien erat, sit amet tempus eros. Aliquam vel risus sed risus egestas molestie. Sed eget fermentum dui. Nunc ac massa ut dolor rhoncus ultrices in at urn", User = new User { UserName = "Sherrif" } },
                                    new IncomeComment
                                        { Comment = "Fuuuu komment", User = new User { UserName = "Dyer" } },
                                    new IncomeComment
                                        { Comment = "Csak a J", User = new User { UserName = "Jocó" } },
                                    new IncomeComment
                                        { Comment = "Le derpina komment", User = new User { UserName = "Ákosch" } }
                                }
                    };
                CommentViewModel = new CommentViewModel(Income.IncomeComments);
            }
        }
    }
}